from django import template
from django.templatetags.static import static

register = template.Library()

@register.simple_tag(takes_context=True)
def render(context, html):
    return template.Template(html).render(context)

@register.simple_tag(takes_context=True)
def page_script_path(context):
    path = context["request"].path
    # remove digit path segments (e.g. '/project/1/plan/' -> '/project/plan/')
    path = '/'.join(seg for seg in path.split('/') if not seg.isdigit())
    if path[-1] == '/':
        return static("script/app" + path + "index.js")
    else:
        # this app's view URL doesn't ends with things like `.html`
        return static("script/app" + path + ".js")
    
    
